import requests
import dealHtml
import dealTxt
import io
import os

def download_ts_files(download_from_url):
    download_url = download_from_url.replace('index.m3u8','')
    path = "temp/ts/"
    txt_name = dealTxt.filename_edited_txt(dealHtml.url)
    if not os.path.exists(path):
        os.mkdir(path)
    f = open("temp/" + txt_name,'r')
    number_of_lines = len(f.readlines())
    f.close()
    session=requests.session()
    f = open("temp/" + txt_name,'r')
    for line in f.readlines():
        line = line.strip()
        r = session.get(download_url + line)
        fr = open(path + line, 'wb')
        print("Downloading %s . Remain %d files." % (line,number_of_lines))
        number_of_lines -= 1
        fr.write(r.content)
        fr.close()
    f.close()
